from PySide6.QtCore import QDateTime, Slot, Qt
from PySide6.QtWidgets import QMessageBox, QDialog, QTableWidgetItem, QMainWindow, QButtonGroup
from PySide6.QtGui import QIcon, QColor

from Devices.Chiot.BsDevs.ComDev.Forms.ComDevWindow import ComDevWindow
from Devices.Chiot.BsDevs.Motion.MotionDevice import MotionDevice
from Devices.Chiot.BsDevs.Motion.MotionParser import MotionParser
from Devices.Chiot.BsDevs.Motion.Forms.MotionWindow_UI import Ui_FormMotionWindow
from Components.MessageBoxFunc import MessageBoxFunc

class MotionWindow(ComDevWindow):
    # 0、窗体初始化、状态变量初始化
    def __init__(self, supportDevice, parent = None):
        # 注意在实现特有设备时，一定要先将其ui对象加载完毕
        super().__init__(supportDevice, parent)

        self.FullStatus = MotionDevice.MotionFullStatus()

        self._ui = Ui_FormMotionWindow()
        self._ui.setupUi(self)
        self.initUiWindow()
        self.SetupUi(self._ui)
        # self.testUi()

    # 1、数据解析
    def setHeartbeatStatus(self):
        pass

    def CmdRptStatusReceived(self, msgValue):
        if MotionParser.ParseHeartbeat(msgValue, self.FullStatus):
            self.setFullStatus()

    def CmdHeartbeatReceived(self, msgValue):
        if MotionParser.ParseHeartbeat(msgValue, self.FullStatus):
            self.setFullStatus()

    def CmdFullStatusReceived(self, msgValue):
        pass

    def CmdMsgValueReceived(self, msgName, msgValue):
        pass

    # 2、运行状态
    def setFullStatus(self):
        self._ui.label_sensorStatus.setText("有人" if self.FullStatus.sensorStatus else "无人")
        self._ui.label_powerWarn.setText("告警" if self.FullStatus.powerWarn else "未告警")
        self._ui.label_batteryPercentage.setText(str(self.FullStatus.batteryPercentage) + "%")
        self._ui.label_checkStatus.setText("正在自检" if self.FullStatus.checkStatus else "未自检")
        self._ui.lineEdit_reportInterval.setText(self.FullStatus.reportInterval)

    @Slot()
    def on_pushButton_setReportInterval_clicked(self):
        interval = int(self._ui.lineEdit_reportInterval.text())
        if interval>0:
            msg = MotionParser.PackSetConfigReportInterval(interval)
            self.SendCommandText(msg)
        else:
            print("interval = [{}] is error.".format(interval))
                              
